package cn.nfwx.tv.base;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.chan.net.HttpCallback;
import com.chan.net.Result;
import com.squareup.leakcanary.RefWatcher;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.umeng.analytics.MobclickAgent;

import cn.nfwx.tv.BuildConfig;
import cn.nfwx.tv.helper.UmengPushHelper;

/**
 * Created by zhouzechao on 2017/12/12.
 */

public abstract class BaseActivity extends RxAppCompatActivity {

    protected Context mContext;
    protected BaseFragment nowFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        UmengPushHelper.getInstance().getPushAgent().onAppStart();
        mContext = this;
        bindView();
        initData();
    }

    protected void bindView() {
    }

    /**
     * 初始化数据
     */
    protected void initData() {
    }

    protected abstract int getLayoutId();

    @Override
    protected void onResume() {
        super.onResume();
        // 友盟统计时长和页面跳转
        MobclickAgent.onResume(this);
        MobclickAgent.onPageStart(activityName());
        //JAnalyticsInterface.onPageStart(this, activityName());

//        if (WifiHelper.getInstance().isShowWifiDialogSuccess()) {
//            WifiHelper.getInstance().clearWifiDialogStatus();
//            new Handler().postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    final WifiConnectDialogFragment wifiConnectDialogFragment = WifiConnectDialogFragment.newInstance(2);
//                    DialogFragmentHelper.showDialogFragment(getSupportFragmentManager(), wifiConnectDialogFragment);
//                    wifiConnectDialogFragment.setSureListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            wifiConnectDialogFragment.dismissAllowingStateLoss();
//                        }
//                    });
//                }
//            }, 1500);
//        }
//        if (WifiHelper.getInstance().isShowWifiDialogFalure()) {
//            WifiHelper.getInstance().clearWifiDialogStatus();
//            new Handler().postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    WifiConnectDialogFragment wifiConnectDialogFragment = WifiConnectDialogFragment.newInstance(3);
//                    DialogFragmentHelper.showDialogFragment(getSupportFragmentManager(), wifiConnectDialogFragment);
//                    wifiConnectDialogFragment.setTryMoreListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            if (!WifiHelper.getInstance().getWifis().isEmpty()) {
//                                WifiHelper.getInstance().connentWifi(WifiHelper.getInstance().getWifis().get(0));
//                            }
//                        }
//                    });
//                }
//            }, 1500);
//        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 友盟统计时长和页面跳转
        MobclickAgent.onPause(this);
        MobclickAgent.onPageEnd(activityName());

        //JAnalyticsInterface.onPageEnd(this, activityName());
    }


    protected abstract String activityName();


    protected void showFragment(BaseFragment fragment, int contentId, String tag) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (nowFragment == null) {
            transaction.add(contentId, fragment, tag).show(fragment);
            nowFragment = fragment;
        } else {
            transaction.hide(nowFragment).replace(contentId, fragment, tag);
            nowFragment = fragment;
        }
        transaction.commitAllowingStateLoss();
    }
}
